class EnemyMate extends ps.Behaviour {
  /** 序列化 */
  private serializableFields: Object = {
    isHorse: qc.Serializer.BOOLEAN,
    life: qc.Serializer.NUMBER,
    attackInterval: qc.Serializer.NUMBER,
    speed: qc.Serializer.NUMBER,
  };

  constructor(gameObject: qc.Node) {
    super(gameObject);
  }

  life: number = 0;
  isHorse: boolean = false;
  mc: ps.MovieClip;
  onInit() {
    this.getMc();
  }

  getMc() {
    console.log("getMc");

    this.mc = this.gameObject.getScript(ps.MovieClip);
  }

  dance() {
    if (!this.mc) this.getMc();
    if (this.isHorse) {
      this.mc.gotoAndPlay(
        "xlz_enemy_horse_dancing/xlz_enemy_horse_dancing_",
        -1
      );
    } else {
      this.mc.gotoAndPlay("xlz_enemy_man_dancing/xlz_enemy_man_dancing_", -1);
    }
  }

  walk() {
    if (!this.mc) this.getMc();
    if (this.isHorse) {
      this.mc.gotoAndPlay("xlz_enemy_horse_walk/xlz_enemy_horse_walk_", -1);
    } else {
      this.mc.gotoAndPlay("xlz_enemy_man_walk/xlz_enemy_man_walk_", -1);
    }
  }

  attack() {
    if (!this.mc) this.getMc();
    if (this.isHorse) {
      this.mc.gotoAndPlay("xlz_enemy_horse_attack/xlz_enemy_horse_attack_", -1);
    } else {
      this.mc.gotoAndPlay("xlz_enemy_man_attack/xlz_enemy_man_attack_", -1);
    }
  }

  die() {
    if (!this.mc) this.getMc();
    if (this.isHorse) {
      this.mc.gotoAndPlay("xlz_enemy_horse_die/xlz_enemy_horse_die_", 1, () => {
        Tween.hideAlpha(this.gameObject, 500, 0, "remove");
      });
    } else {
      this.mc.gotoAndPlay("xlz_enemy_man_die/xlz_enemy_man_die_", 1, () => {
        Tween.hideAlpha(this.gameObject, 500, 0, "remove");
      });
    }
    ps.Audio.playSound(`sound_die_${Random.range(1, 8, "round")}`);
  }

  idle() {
    if (!this.mc) this.getMc();
    if (this.isHorse) {
      this.mc.gotoAndPlay("xlz_enemy_horse_idle/xlz_enemy_horse_idle_", -1);
    } else {
      this.mc.gotoAndPlay("xlz_enemy_man_idle/xlz_enemy_man_idle_", -1);
    }
  }

  getNearest() {
    let minDistance: number = Number.MAX_VALUE;
    let target: qc.Node;
    const myPos: qc.Point = this.gameObject.getWorldPosition();
    main.team.forEach((m) => {
      const distance: number = m.getWorldPosition().distance(myPos);
      if (distance < minDistance) {
        minDistance = distance;
        target = m;
      }
    });
    return {
      target,
      distance: minDistance,
    };
  }

  attacking: boolean = false;
  speed: number = 2;
  attackInterval: number;
  attackLoop: qc.TimerEvent;

  hurt() {
    if (this.life <= 0) return;
    this.life--;
    main.progressWu.value--;
    if (this.life <= 0) {
      this.attacking = false;
      this.attackTarget = null;
      main.enemies.splice(main.enemies.indexOf(this.gameObject), 1);
      this.die();
    }
  }

  attackTarget: qc.Node;

  update() {
    if (main.attacking) {
      if (this.attackTarget) {
        if (!this.attacking) {
          if (this.attackLoop) {
            this.game.timer.remove(this.attackLoop);
            this.attackLoop = null;
          }
          if (this.attackTarget.getScript(TeamMate).life <= 0) {
            main.findTarget(this.gameObject, main.team, EnemyMate);
          } else {
            main.approach(this.gameObject, this.attackTarget, this.speed);
          }
          if (
            this.attackTarget &&
            main.getDistance(this.gameObject, this.attackTarget) <=
              main.attackMinDistance
          ) {
            main.attackStart(
              this.gameObject,
              EnemyMate,
              TeamMate,
              main.team,
              this.attackTarget
            );
          }
        }
      }
    }
  }
}
qc.registerBehaviour("EnemyMate", EnemyMate);
